package pool;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class PoolTest {

    @Test
    public void testThreadPoolExecutor() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5));
//        ExecutorService executor = Executors.newFixedThreadPool(15);
        for (int i = 0; i < 16; i++) {
            int id = i;
            Thread thread = new Thread(() -> {
                System.out.println("start thread " + id);
                try {
                    Thread.currentThread().sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end thread " + id);
            });
            // execute()用于执行无返回值的任务，submit()用于执行有返回值的任务
            executor.execute(thread);
//            Future<?> future = executor.submit(thread);
            System.out.println("线程池中线程数：" + executor.getPoolSize() + "，等待执行的任务数：" +
                    executor.getQueue().size() + "，已执行的任务数：" + executor.getCompletedTaskCount());
        }
        executor.shutdown();
        Thread.sleep(10000);
    }
}
